package cn.doitedu.day02

import scala.collection.mutable.ArrayBuffer

/**
 * flatten，压平，打平，即将一个集合中嵌套的子集合进行压平
 */
object FlattenDemo {


  def main(args: Array[String]): Unit = {

    val arr: Array[Array[Int]] = Array(Array(1,2,3), Array(4,5,6), Array(7,8,9))

    //现在希望将大Array中的小Array给压平
    val res: Array[Int] = arr.flatten

    println(res.toBuffer)


    //底层实现就是进行了++=
    val ab = new ArrayBuffer[Int]()

    for (e <- arr) {
      ab ++= e
    }

    println(ab.toArray.toBuffer)


    //期望将数组中的多个单词，进行拆解，最终返回的是数组中装的一个一个的单词
    val lines: Array[String] = Array("spark hadoop flink", "spark hive", "hadoop spark spark", "flink spark hive flink flink hive")
    //Array("spark", "hadoop", "flink", "spark", "hive", ...)

    val wordArr: Array[Array[String]] = lines.map(_.split(" "))

    val res2: Array[String] = wordArr.flatten

    println(res2.toBuffer)




  }

}
